﻿using DotNetMVC.IService;
using DotNetMVC.Website.Areas.WebAdmin.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;

namespace DotNetMVC.Website.Areas.WebAdmin.Common
{
    public class AdminHelper
    {
        public static long? GetUserId(HttpContextBase ctx)
        {
            return (long?)ctx.Session["LoginUserId"];
        }

        public static string GetUserName(HttpContextBase ctx)
        {
            if (ctx.Session["LoginUserName"]==null)
            {
                return null;
            }
            return ctx.Session["LoginUserName"].ToString();
        }

        /// <summary>
        /// 用户操作日志
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="msg"></param>
        public static void AddUserLog(HttpContextBase ctx,string msg)
        {
            IAdminLogService adminLogService = DependencyResolver.Current.GetService<IAdminLogService>();
            adminLogService.Add(GetUserId(ctx).Value, msg);
        }

        #region 网站配置
        /// <summary>
        /// 获取网站配置信息
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static SiteConfig GetSiteConfig(ControllerContext context)
        {
            string cacheKey = "AdminHelper_SiteConfig";
            //先尝试去缓存中找
            SiteConfig model = (SiteConfig)context.HttpContext.Cache[cacheKey];
            if (model != null)
            {
                return model;
            }

            model = new SiteConfig();
            string path = context.HttpContext.Server.MapPath("~/App_Data/SiteConfig.xml");
            XDocument xDoc = XDocument.Load(path);
            XElement root = xDoc.Root;
            model.SiteName = GetAttributeValue(root, "sitename");
            model.SiteUrl = GetAttributeValue(root, "siteurl");
            model.Keywords = GetAttributeValue(root, "keywords");
            model.Description = GetAttributeValue(root, "description");
            model.Homebanner = GetAttributeValue(root, "homebanner");
            model.HomeNotice = GetAttributeValue(root, "homeNotice");
            model.HomeNews = GetAttributeValue(root, "homeNews");
            model.HomeAbout = GetAttributeValue(root, "homeAbout");
            model.HomeStudy = GetAttributeValue(root, "homeStudy");
            model.Headerlinks = GetAttributeValue(root, "headerlinks");
            model.Footerlinks = GetAttributeValue(root, "footerlinks");
            model.Address = GetAttributeValue(root, "address");
            model.Tel = GetAttributeValue(root, "tel");
            model.Email = GetAttributeValue(root, "email");
            model.Copyright = GetAttributeValue(root, "copyright");

            //存入缓存
            context.HttpContext.Cache.Insert(cacheKey, model, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero);
            return model;
        }

        /// <summary>
        /// 设置网站配置信息
        /// </summary>
        /// <param name="context"></param>
        /// <param name="sitename"></param>
        /// <param name="siteurl"></param>
        /// <param name="keywords"></param>
        /// <param name="description"></param>
        /// <param name="homebanner"></param>
        /// <param name="homeNotice"></param>
        /// <param name="homeNews"></param>
        /// <param name="homeAbout"></param>
        /// <param name="homeStudy"></param>
        /// <param name="headerlinks"></param>
        /// <param name="footerlinks"></param>
        /// <param name="address"></param>
        /// <param name="tel"></param>
        /// <param name="email"></param>
        /// <param name="copyright"></param>
        public static void SetSiteConfig(ControllerContext context, string sitename, string siteurl, string keywords, string description,
            string homebanner, string homeNotice, string homeNews, string homeAbout, string homeStudy, 
            string headerlinks, string footerlinks,string address, string tel, string email, string copyright)
        {
            string path = context.HttpContext.Server.MapPath("~/App_Data/SiteConfig.xml");
            XDocument xDoc = XDocument.Load(path);
            XElement root = xDoc.Root;
            SetAttributeValue(root, "sitename", sitename);
            SetAttributeValue(root, "siteurl", siteurl);
            SetAttributeValue(root, "keywords", keywords);
            SetAttributeValue(root, "description", description);
            SetAttributeValue(root, "homebanner", homebanner);
            SetAttributeValue(root, "homeNotice", homeNotice);
            SetAttributeValue(root, "homeNews", homeNews);
            SetAttributeValue(root, "homeAbout", homeAbout);
            SetAttributeValue(root, "homeStudy", homeStudy);
            SetAttributeValue(root, "headerlinks", headerlinks);
            SetAttributeValue(root, "footerlinks", footerlinks);
            SetAttributeValue(root, "address", address);
            SetAttributeValue(root, "tel", tel);
            SetAttributeValue(root, "email", email);
            SetAttributeValue(root, "copyright", copyright);
            xDoc.Save(path);

            #region 更新缓存
            SiteConfig model = new SiteConfig();
            model.SiteName = sitename;
            model.SiteUrl = siteurl;
            model.Keywords = keywords;
            model.Description = description;
            model.Homebanner = homebanner;
            model.HomeNotice = homeNotice;
            model.HomeNews = homeNews;
            model.HomeAbout = homeAbout;
            model.HomeStudy = homeStudy;
            model.Headerlinks = headerlinks;
            model.Footerlinks = footerlinks;
            model.Address = address;
            model.Tel = tel;
            model.Email = email;
            model.Copyright = copyright;
            string cacheKey = "AdminHelper_SiteConfig";
            //存入缓存
            context.HttpContext.Cache.Insert(cacheKey, model, null, DateTime.Now.AddMinutes(30), TimeSpan.Zero); 
            #endregion
        }


        /// <summary>
        /// 获取节点属性值
        /// </summary>
        /// <param name="root"></param>
        /// <param name="elementName"></param>
        /// <returns></returns>
        private static string GetAttributeValue(XElement root, string elementName)
        {
            return root.Elements(elementName).Single().Attribute("value").Value;
        }

        /// <summary>
        /// 设置节点属性值
        /// </summary>
        /// <param name="root"></param>
        /// <param name="elementName"></param>
        /// <param name="elementValue"></param>
        private static void SetAttributeValue(XElement root, string elementName, string elementValue)
        {
            root.Elements(elementName).Single().SetAttributeValue("value", elementValue);
        } 
        #endregion
    }
}